package com.xplay.xpocker.controller;

import com.xplay.xpocker.config.ResultFul;
import com.xplay.xpocker.config.security.CurrentUser;
import com.xplay.xpocker.constant.RoomTypeEnum;
import com.xplay.xpocker.dvo.AbstractRoom;
import com.xplay.xpocker.entity.GameRuRoom;
import com.xplay.xpocker.exception.ServiceAssertion;
import com.xplay.xpocker.exception.StatusCodeEnum;
import com.xplay.xpocker.room.RoomStrategy;
import com.xplay.xpocker.room.message.MessageContent;
import com.xplay.xpocker.service.gaming.GameRuRoomService;
import com.xplay.xpocker.service.gaming.GameRuRoomUserService;
import com.xplay.xpocker.util.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wanjie
 * @date 2021/3/17 20:36
 */
@Slf4j
@RestController
@RequestMapping(value = "/room", produces = "application/json;charset=UTF-8")
public class RoomController {
    private static final Logger logger = LoggerFactory.getLogger(RoomController.class);
    @Autowired
    private GameRuRoomService roomService;
    @Resource
    private GameRuRoomUserService roomUserService;


    @PostMapping(value = "create")
    public ResultFul<GameRuRoom> createRoom(@RequestBody AbstractRoom roomEntity) {
        RoomTypeEnum roomTypeEnum = RoomTypeEnum.of(roomEntity.getType());
        RoomStrategy strategy = RoomStrategy.getRoomStrategy(roomTypeEnum.getParent());
        Assertion.isNull(strategy, "非法的业务创建");
        return ResultFul.ok(strategy.create(roomEntity));
    }

    @GetMapping(value = "read")
    public ResultFul<AbstractRoom> readRoom(String roomCode) {
        RoomStrategy strategy = RoomStrategy.getRoomStrategy(roomCode);
        Assertion.isNull(strategy, "非法的业务创建");
        AbstractRoom room = strategy.currentLoadRoom(roomCode, CurrentUser.getUserId());
        return ResultFul.ok(room);
    }


    @PostMapping(value = "exit")
    public ResultFul<Boolean> exitRoom() {
        GameRuRoom gameRuRoom = roomService.currentUserRoom();
        if (gameRuRoom == null) {
            return ResultFul.ok(true);
        }
        RoomTypeEnum roomTypeEnum = RoomTypeEnum.of(gameRuRoom.getType());
        RoomStrategy strategy = RoomStrategy.getRoomStrategy(roomTypeEnum.getParent());
        Assertion.isNull(strategy, "非法的业务创建");
        strategy.exitRoom(gameRuRoom, CurrentUser.getUserId());
        return ResultFul.ok(true);
    }


    @GetMapping(value = "get")
    public ResultFul<GameRuRoom> getRoom() {
        return ResultFul.ok(roomService.currentUserRoom());
    }

    @PostMapping(value = "add")
    public ResultFul<GameRuRoom> addRoom(String roomCode) {
        RoomStrategy strategy = RoomStrategy.getRoomStrategy(roomCode);
        Assertion.isNull(strategy, "非法的业务创建");
        return ResultFul.ok(strategy.addRoom(roomCode));
    }

    /**
     * 一级策略  处理    消息类型 语音、消息、事件   AbstractMessageStrategy
     * 一级策略实现        Action、Chat、VoiceMessageService
     * 二级策略  处理    一级策略的子类型 比如什么事件 AbstractActionStrategy
     * 二级策略实现
     * source 当前事件执行人
     * roomCode 用于消息转发
     *
     * @param data
     * @return
     */

    @PostMapping(value = "message")
    public ResultFul messageRoom(@RequestBody MessageContent<Object> data) {
        // 房间内所有的消息 统一处理
        // 找到用户所属房间号 顶层类型  1 语音   2   消息 统一处理  3  事件   内部消化
        // 按照类型处理
        GameRuRoom userRoom = roomService.currentUserRoom();
        ServiceAssertion.isNull(userRoom, StatusCodeEnum.ROOM_NOT_EXISTS);
        data.setMessageFromUid(CurrentUser.getUserId());
        data.setRoomCode(userRoom.getCode());
        roomService.doMessage(data);
        return ResultFul.ok(true);
    }

}
